﻿using System;
using System.Diagnostics;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
using AMS.DeDuplicator.CommonLayer;

namespace AMS.DeDuplicator.BusinessLayer.Command
{
    public class OpenFileCommand : Command
    {
        string _path;
        Process _process;

        public OpenFileCommand(string path)
        {
            InputValidator.ValidateNotNull(path);
            InputValidator.ValidateFileExists(path);
            _path = path;
        }

        public override bool Execute()
        {
            if (_process != null && !_process.HasExited)
            {
                // already running;
                return true;
            }

            // Open the file with its default
            try
            {
                _process = Process.Start(_path);
                this.Executed = true;
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public override bool Undo()
        {
            if (!Executed)
                ExceptionHandler.NotExecutedException();

            if (!_process.HasExited)
            {
                _process.CloseMainWindow();
            }

            this.Executed = false;
            return true;
        }
    }
}
